package pos.dato;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import pos.dominios.Perfil;
import pos.dominios.Usuario;


public class JDBCUsuarioDAO implements IUsuarioDAO {
    IPerfilDAO perdao;
    
    public JDBCUsuarioDAO() {
        perdao = new JDBCPerfilDAO();
       
    }

    public void delete(Connection conn, String usuarioID) {
        String sql = "DELETE FROM Usuarios WHERE (usuarioID = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuarioID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }

  
    public Usuario select(Connection conn, String usuarioID) {
        Usuario usuario = new Usuario();
        String sql = "SELECT * FROM Usuarios WHERE (usuarioID = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuarioID);
            result = stmt.executeQuery();
            result.next();
            usuario.setNick(result.getString("nick"));
            usuario.setPassword(result.getString("password"));
            Perfil p = perdao.select(conn, result.getString("perfilID"));
            usuario.setPerfil(p);
            usuario.setUsuarioID(result.getString("usuarioID"));
 
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return usuario;
    }
    

    public List<Usuario> selectAllUsuarios() {
        Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List<Usuario> searchResults = new LinkedList<Usuario>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Usuarios";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Usuario temp = new Usuario();
                temp.setNick(result.getString("nick"));
                temp.setPassword(result.getString("password"));
                Perfil p = perdao.select(conn, result.getString("perfilID"));
                temp.setPerfil(p);
                temp.setUsuarioID(result.getString("usuarioID"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }
    
    public void updateKarma( String perfilID, String a){
    	PreparedStatement stmt = null;
    	Connection conn = ConnectionManager.getInstance().checkOut();
    	try {
    		String sql ="UPDATE Perfiles SET karma="+a+" WHERE perfilID='"+perfilID+"'";
            stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }    
}



